<template>
  <div>
    <el-dialog title="编辑用户" :visible.sync="editdialogVisible" :before-close="close" width="600px">
      <el-form style="padding-right: 45px;" :model="userModel" :rules="rules" ref="userModel" label-width="100px" class="demo-userModel">
        <el-form-item label="用户名">
          <el-input v-model="userModel.username" disabled=""></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="userModel.realname"></el-input>
        </el-form-item>
        <el-form-item label="角色分配" prop="roles">
          <el-checkbox-group v-model="userRole">
            <el-checkbox v-for="item in roleList" :key="item.id" name="Roles" :label="item.id">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <el-form-item label="区选择">
            <el-select v-model="userModel.regionid" placeholder="请选择区" @change="getStreetIdSelect">
              <el-option v-for="item in editRegionIdSelect" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="街道选择">
            <el-select v-model="userModel.streetid" placeholder="请选择街道" @change="getNeighborhoodCommitteeIdSelect">
              <el-option v-for="item in editStreetIdSelect" :key="item.id" :label="item.streetname" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="居委选择">
            <el-select v-model="userModel.neighborhoodcommitteeid" placeholder="请选择居委">
              <el-option v-for="item in editNeighborhoodCommitteeIdSelect" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div class="dialog-footer text-center mt-3">
        <el-button type="primary" @click="submitForm('userModel')">确认</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    GetRegionPaged,
    getJuWeis,
    getStreePaged
  } from '@/api/address.js'
  export default {
    props: ["editdialogVisible", "userModel"], //接受父组件的值
    data() {
      return {
        rules: {
          RealName: [{
            required: true,
            message: "请输入真实姓名",
            trigger: "blur"
          }],
          Roles: [{
            required: true,
            message: "请选择角色",
            trigger: "blur"
          }]
        },
        roleList: [],
        userRole: [],
        editRegionIdSelect: [],
        editStreetIdSelect: [],
        editNeighborhoodCommitteeIdSelect: []
      };
    },

    created() {
      console.log(this.userModel)
      this.getRegionIdSelect();
      this.getStreetIdSelect();
      this.getNeighborhoodCommitteeIdSelect();
      this.getRoles();
      this.getUserRoles();
    },

    methods: {
      getRoles: function() {
        this.$api.get("/api/Role/getRoleAll", null).then(rsp => {
          if (rsp != false) {
            this.roleList = rsp.data;
            console.log(this.roleList.length);
          }
        });
      },
      getRegionIdSelect() {
        GetRegionPaged({
          pageIndex: 1,
          pageSize: 999
        }).then(res => {
          this.editRegionIdSelect = res.data.list
        })
      },
      getStreetIdSelect() {
        getStreePaged({
          pageIndex: 1,
          pageSize: 999,
          RegionId: this.userModel.regionid
        }).then(res => {
          this.editStreetIdSelect = res.data.list
        })

      },
      getNeighborhoodCommitteeIdSelect() {
        getJuWeis({
          pageIndex: 1,
          pageSize: 999,
          streetid: this.userModel.streetid
        }).then(res => {
          this.editNeighborhoodCommitteeIdSelect = res.data.list
        })
      },
      getUserRoles: function() {
        this.$api.get("/api/User/getUserRoles", {
          userId: this.userModel.id
        }).then(rsp => {
          if (rsp != false) {
            this.userRole = rsp.data;
          }
        });
      },

      submitForm: function(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editModel = {
              userid: this.userModel.id,
              realname: this.userModel.realname,
              roles: this.userRole,
              username: this.userModel.username,
              regionid: this.userModel.regionid,
              streetid: this.userModel.streetid,
              neighborhoodcommitteeid:this.userModel.neighborhoodcommitteeid
            };
            this.$api.post("/api/User/update", editModel, true).then(rsp => {
              if (rsp != false) {
                this.close();
                this.$emit("successEditUser");
              }
            });
          }
        });
      },

      close() {
        this.$emit("closeEditUser");
      }
    }
  };
</script>
